package com.zsl.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.zsl.dao.BookDao;
import com.zsl.dao.CategoryDao;
import com.zsl.dao.impl.BookDaoImpl;
import com.zsl.dao.impl.CategoryDaoImpl;
import com.zsl.model.Book;
import com.zsl.model.Category;
import com.zsl.service.CategoryService;
import com.zsl.util.Util;

import java.sql.SQLException;
import java.util.*;

public class CategoryServiceImpl implements CategoryService {
    CategoryDao categoryDao = new CategoryDaoImpl();
    BookDao bookDao = new BookDaoImpl();
    public static String DEFAULT_CATEGORY_ID = "ffffffff-ffff-ffff-ffff-ffffffffffff";

    @Override
    public Map<String, Object> getAllCategory() throws SQLException {
        Map<String, Object> result = new HashMap<>();
        JSONArray rows = new JSONArray();
        int total = 0;

        //获取所有的目录数据
        List<Category> categories = categoryDao.getCategory(null, null);
        if (categories.isEmpty()) {
            result.put("total", 0);
            result.put("rows", rows);
            return result;
        }
        for (Category category : categories) {
            Map<String, Object> category_map = new HashMap<>();
            category_map.put("c_id", category.getId());
            category_map.put("c_name", category.getName());
            category_map.put("c_description", category.getDescription());
            rows.add(category_map);
            total++;
        }
        result.put("total", total);
        result.put("rows", rows);
        return result;
    }

    @Override
    public Map<String, Object> insertCategory(String c_name, String c_description) throws SQLException {
        Map<String, Object> result = new HashMap<>();
        //返回错误
        if (c_name == null || c_description == null) {
            result.put("code", 0);
            result.put("msg", "参数错误");
            result.put("line", 0);
            return result;
        }
        // 判断这个分类名是否已经存在
        if (categoryDao.isExistCategory(c_name)) {
            result.put("code", 0);
            result.put("msg", "重复分类名");
            result.put("line", 0);
            return result;
        }
        //执行插入操作
        final Category category = new Category();
        category.setId(UUID.randomUUID().toString());
        category.setName(c_name);
        category.setDescription(c_description);

        Map<String, Object> r = categoryDao.insertCategory(
                new ArrayList<Category>() {{
                    add(category);
                }}
        );
        List<Integer> line = Util.castList(r.get("line"), Integer.class);
        if (line.isEmpty()) {
            result.put("code", 0);
            result.put("msg", "更新失败");
            result.put("line", 0);
            return result;
        }
        if (line.get(0) == 0) {
            result.put("code", 0);
            result.put("msg", "更新失败");
            result.put("line", 0);
            return result;
        }
        result.put("code", 1);
        result.put("msg", "更新成功");
        result.put("line", 1);
        return result;
    }

    //TODO 未完成（更新分类
    @Override
    public Map<String, Object> updateCategory(String c_name, String c_description) {
        Map<String, Object> result = new HashMap<>();
        //返回错误
        if (c_name == null || c_description == null) {
            result.put("code", 0);
            result.put("msg", "参数错误");
            result.put("line", 0);
            return result;
        }


        return null;
    }

    @Override
    public Map<String, Object> deleteCategory(String c_id) throws SQLException {
        Map<String, Object> result = new HashMap<>();
        // 获取使用这个分类的所有书籍
        List<Book> books = bookDao.getBook(
                new String[]{"category_id"},
                new String[]{c_id}
        );
        //当这本书不为空的时候,修改其分类为默认分类
        if (!books.isEmpty()) {
            for (Book book : books) {
                book.setCategory_id(DEFAULT_CATEGORY_ID);
            }
            Map<String, Object> r = bookDao.updateBookById(books, 1);
            List<Integer> line = Util.castList(r.get("line"), Integer.class);
            for (Integer l : line) {
                if (l == 0) {
                    result.put("code", 0);
                    result.put("msg", "修改出错");
                    result.put("line", 0);
                    return result;
                }
            }
        }
        //删除分类
        int l = categoryDao.deleteCategoryById(c_id);
        if(l==0){
            result.put("code", 0);
            result.put("msg", "删除出错");
            result.put("line", 0);
            return result;
        }
        result.put("code", 1);
        result.put("msg", "删除成功");
        result.put("line", 1);
        return result;
    }

}
